package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xubeizhi.common.ResponseResult;
import com.xubeizhi.common.SqlConf;
import com.xubeizhi.entity.FriendLink;
import com.xubeizhi.mapper.FriendLinkMapper;
import com.xubeizhi.service.FriendLinkService;
import com.xubeizhi.utils.PageUtils;
import com.xubeizhi.vo.FriendLinkVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

import static com.xubeizhi.enums.FriendLinkEnum.APPLY;
import static com.xubeizhi.enums.FriendLinkEnum.UP;

/**
 * 友情链接表 服务实现类
 * @author mi
 * @create 2023/3/7 14:18
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FriendLinkServiceImpl extends ServiceImpl<FriendLinkMapper, FriendLink> implements FriendLinkService {
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 友情链接列表
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResponseResult listFriendLink(String name, Integer status) {
        QueryWrapper<FriendLink> queryWrapper = new QueryWrapper<FriendLink>()
                .orderByDesc(SqlConf.SORT).like(StringUtils.isNotBlank(name),SqlConf.NAME,name)
                .eq(status != null,SqlConf.STATUS,status);
        Page<FriendLink> friendLinkPage = baseMapper.selectPage(new Page<>(PageUtils.getPageNo(),PageUtils.getPageSize()),queryWrapper);

        return ResponseResult.success(friendLinkPage);
    }

    /**
     * 添加友情链接
     * @param friendLink
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult insertFriendLink(FriendLink friendLink) {
        baseMapper.insert(friendLink);
        return ResponseResult.success();
    }

    /**
     * 修改友链
     * @param friendLink
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateFriendLink(FriendLink friendLink) {
//        baseMapper.updateById(friendLink);
//        //审核通过发送邮箱通知
//        if (friendLink.getStatus().equals(UP.getCode())){
//
//        }
        return null;
    }

    /**
     * 删除友情链接
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteBatch(List<Integer> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        return rows > 0 ? ResponseResult.success(): ResponseResult.error("删除友情链接失败!");
    }

    /**
     * 置顶友情链接
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult top(Integer id) {
        Integer sort = baseMapper.getMaxSort();
        baseMapper.top(id,sort+1);
        return ResponseResult.success();
    }

    //-----------web端方法开始-----------

    /**
     * 友链列表
     * @return
     */
    @Override
    public ResponseResult webFriendLinkList() {
        List<FriendLinkVO> list = baseMapper.selectLinkList(UP.code);
        return ResponseResult.success();
    }

    /**
     * 申请友链
     * @param friendLink
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult applyFriendLink(FriendLink friendLink) {
        Assert.isTrue(StringUtils.isNotBlank(friendLink.getUrl()),"请输入正确的网址!");
        friendLink.setStatus(APPLY.getCode());

        Assert.isTrue(!friendLink.getUrl().contains("xubeizhi.cn"),"不合法的网址!");

        //如果已经申请过友情链接 再次介入则会进行下架处理 需重新审核
        FriendLink entity = baseMapper.selectOne(new QueryWrapper<FriendLink>()
                .eq(SqlConf.URL,friendLink.getUrl()));
        if (entity != null){
            friendLink.setId(entity.getId());
        }else {
            baseMapper.insert(friendLink);
        }

        //不影响用户体验 新一个线程操作邮箱发送
//        threadPoolTaskExecutor.execute(() -> );
        return ResponseResult.success();
    }
}
